import requests
from bs4 import BeautifulSoup
import time

def get_movie_links(base_url, max_page=3):
    """获取分页列表中的电影链接"""
    all_links = []
    
    for page in range(1, max_page + 1):
        list_url = f"{base_url}page/{page}"
        try:
            response = requests.get(list_url, timeout=10)
            response.raise_for_status()
            
            soup = BeautifulSoup(response.text, 'lxml')
            print(f"正在解析分页列表页 {page}...")
            
            # 提取当前页所有电影链接
            link_elements = soup.select('a.name[href^="/detail/"]')
            page_links = [f"{base_url}{el['href'].lstrip('/')}" for el in link_elements]
            
            all_links.extend(page_links)
            print(f"第 {page} 页找到 {len(page_links)} 部电影")
            
            time.sleep(1)
            
        except Exception as e:
            print(f"分页 {page} 处理失败: {str(e)}")
    
    print(f"\n共发现 {len(all_links)} 部电影")
    return all_links

def scrape_movie_detail(url):
    """爬取单个电影详情页"""
    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()
        
        soup = BeautifulSoup(response.text, 'lxml')
        
        return {
            "title": soup.find('h2', class_='m-b-sm').get_text(strip=True),
            "score": soup.find('p', class_='score').get_text(strip=True),
            "intro": soup.find('div', class_='drama').find('p').get_text(strip=True),
            "url": url
        }
    
    except Exception as e:
        print(f"电影页 {url} 爬取失败: {str(e)}")
        return None

def main():
    base_url = "https://ssr1.scrape.center/"
    
    # 第一步：获取所有电影链接
    print("====== 开始爬取分页列表 ======")
    movie_links = get_movie_links(base_url, max_page=3)
    
    # 第二步：爬取所有详情页
    print("\n====== 开始爬取详情页 ======")
    movies_data = []
    
    for i, link in enumerate(movie_links, 1):
        print(f"\n正在处理第 {i}/{len(movie_links)} 部电影...")
        movie_data = scrape_movie_detail(link)
        
        if movie_data:
            movies_data.append(movie_data)
            print(f"标题: {movie_data['title']}")
            print(f"评分: {movie_data['score']}")
            print("简介:")
            print(movie_data['intro'] + "\n" + "-"*50)
            
        time.sleep(1)
    
    print("\n====== 爬取完成 ======")
    print(f"成功获取 {len(movies_data)}/{len(movie_links)} 部电影数据")

if __name__ == "__main__":
    main()